package com.roy.base;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;

import java.util.concurrent.TimeUnit;

/**
 * @author 山顶洞人郑某
 * @className StreamingDemo.java 流式问答
 * @date 2025 03 31
 */
public class StreamingDemo {
    public static void main(String[] args) {
        OpenAiStreamingChatModel model = OpenAiStreamingChatModel.builder()
                .baseUrl("https://api.aihao123.cn/luomacode-api/open-api")
                .apiKey("h6ng15qgr9klhcphh058pckmm4gk2npqq2ohv765f5780h84")
                .build();
        model.generate("你好，你是谁？", new StreamingResponseHandler<AiMessage>() {
            @Override
            public void onNext(String token) {
                System.out.println(token);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onError(Throwable error) {
                System.out.println("error"+error.getMessage());
            }
        });
    }
}
